package edu.jsnu.purchase.ticket.service.impl;

import edu.jsnu.purchase.ticket.service.OrderService;
import edu.jsnu.service.ticket.film.dao.FilmMapper;
import edu.jsnu.service.ticket.film.domain.Film;
import edu.jsnu.service.ticket.order.dao.OrderMapper;
import edu.jsnu.service.ticket.order.domain.Order;
import edu.jsnu.service.ticket.order.dto.OrderShow;
import edu.jsnu.service.ticket.order.dto.OrderVo;
import edu.jsnu.service.ticket.seat.dao.SeatMapper;
import edu.jsnu.service.ticket.user.dao.UserMapper;
import edu.jsnu.service.ticket.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("purchaseOrderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SeatMapper seatMapper;
    @Autowired
    private FilmMapper filmMapper;

    @Override
    public List<OrderVo> getUserOrderInfo(Integer userId) {
        List<OrderVo> orderList = orderMapper.getOrderInfoByUserId(userId);
        for (OrderVo schedule : orderList) {
            try {
                if (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(schedule.getStartTime()).getTime() - new Date().getTime() >= 0 && schedule.getStatus().equals("已支付")){
                    schedule.setReback(true);
                }else {
                    schedule.setReback(false);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return orderList;
    }

    @Override
    public OrderShow getOrderInfoById(Integer id) {
        OrderShow order = orderMapper.selectOrderById(id);
        return order;
    }

    @Override
    public Order insertOrder(Integer userId, Integer scheduleId, Integer filmId, Integer quantity, Double money, String seatNo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date now = new Date();
        Order order = new Order();
        order.setUserId(userId);
        order.setScheduleId(scheduleId);
        order.setFilmId(filmId);
        order.setOrderNo("YE"+ sdf.format(now));
        order.setOrderTime(now);
        order.setQuantity(quantity);
        order.setMoney(money);
        order.setStatus(0);//0未支付，1已支付，2订单关闭
        order.setSeatNo(seatNo);
        orderMapper.insertSelective(order);
        //film的销量加1
        Film f = filmMapper.selectByPrimaryKey(filmId);
        f.setSales(f.getSales() + 1);
        filmMapper.updateByPrimaryKey(f);
        return order;
    }

    //用户退票
    @Override
    public Integer updateOrderStatusByUser(Integer orderId, Integer status) {
        //关闭订单
        Integer result = orderMapper.updateOrderStatus(orderId, status);
        //退回用户的费用
        Order order = orderMapper.selectByPrimaryKey(orderId);
        User user = null;
        user = userMapper.selectByPrimaryKey(order.getUserId());
        if (user != null){
            user.setBalance(order.getMoney() + user.getBalance());
            result = userMapper.updateByPrimaryKey(user);
        }
        //删除座位选中信息
        seatMapper.deleteByOrderId(orderId);
        return result;
    }


}
